package com.zhixiang.xaec.service.common.impl;

import java.text.NumberFormat;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhixiang.core.dao.BaseDao;
import com.zhixiang.core.service.impl.BaseServiceImpl;
import com.zhixiang.xaec.dao.common.ListnoDao;
import com.zhixiang.xaec.entity.common.Listno;
import com.zhixiang.xaec.service.common.ListnoService;

@Service
public class ListnoServiceImpl extends BaseServiceImpl<Listno, Long> implements ListnoService
{

    @Resource(type=ListnoDao.class)
    public void setBaseDao(BaseDao<Listno, Long> dao)
    {
        setDao(dao);
    }

    @Override
    @Transactional
    public String getListNo(String predix)
    {
        String hql = "FROM Listno where lnIndex = ?";
        Listno no = uniqueResult(hql, new Object[]{predix});
        
        String result = "";
        
        NumberFormat ft1 = NumberFormat.getInstance();
        ft1.setMinimumIntegerDigits(10);
        result = no.getLnIndex() + ft1.format(no.getLnValue()).replace(",", "");
        
        if(addListNo(predix)){
            return result;
        }
        
        return "";
    }

    @Override
    public boolean addListNo(String predix)
    {
        String sql = "UPDATE Listno set lnValue = (lnValue+1) where lnIndex = ?";
        Long count = update(sql, new Object[]{predix});
        return count > 0 ? true : false;
    }
    
    public static void main(String[] args)
    {
        String result = "";
        String lnIndex = "JH";
        int lnValue = 543;
        
        NumberFormat ft1 = NumberFormat.getInstance();
        ft1.setMinimumIntegerDigits(6);
        result = lnIndex + ft1.format(lnValue).replace(",", "");
        System.out.println(result);
    }
    
}
